package net.arvin.dao;

import java.util.List;

import net.arvin.mobilebanksystem.R;
import net.arvin.model.TransfersBill;
import net.arvin.utils.TransfersType;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LatestTransferAdapter extends BaseAdapter {
	private List<TransfersBill> mBill;
	private LayoutInflater mInflater;

	public LatestTransferAdapter(List<TransfersBill> mBill, Context context) {
		this.mInflater = LayoutInflater.from(context);
		this.mBill = mBill;
	}

	@Override
	public int getCount() {
		if (mBill == null) {
			return 0;
		}
		return this.mBill.size();
	}

	@Override
	public Object getItem(int position) {
		return mBill.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CardsInfoHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listitem_latest_transfer,
					null);
			holder = new CardsInfoHolder();
			holder.bankIcon = (ImageView) convertView
					.findViewById(R.id.bank_icon);
			holder.nickname = (TextView) convertView
					.findViewById(R.id.latest_nickname);
			holder.phonenumber = (TextView) convertView
					.findViewById(R.id.latest_phonenumber);
			convertView.setTag(holder);
		} else {
			holder = (CardsInfoHolder) convertView.getTag();
		}
		if (mBill.get(position).getCardOrUser().get(0)
				.equals(TransfersType.U2U)) {
			holder.bankIcon.setImageResource(R.drawable.avatar);
			holder.nickname.setText(mBill.get(position).getNickname().get(0));
			holder.phonenumber.setText(mBill.get(position).getPhonenumber()
					.get(0));
		} else {
			holder.bankIcon.setImageResource(R.drawable.bank_icon);
			String cardBank = mBill.get(position).getCardBank().get(0);
			String nickname = mBill.get(position).getNickname().get(0);
			holder.nickname.setText(nickname);
			String cardNum = mBill.get(position).getCardnumber().get(0);
			holder.phonenumber.setText(cardBank + "("
					+ cardNum.substring(cardNum.length() - 3) + ")");
		}
		return convertView;
	}

	private class CardsInfoHolder {
		public ImageView bankIcon;
		public TextView nickname;
		public TextView phonenumber;
	}

}
